package ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import net.ck.hzq.R;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import base.BaseFra;
import butterknife.BindView;
import butterknife.ButterKnife;
import cn.finalteam.loadingviewfinal.HeaderAndFooterRecyclerViewAdapter;
import cn.finalteam.loadingviewfinal.OnDefaultRefreshListener;
import cn.finalteam.loadingviewfinal.OnLoadMoreListener;
import cn.finalteam.loadingviewfinal.PtrClassicFrameLayout;
import cn.finalteam.loadingviewfinal.PtrFrameLayout;
import cn.finalteam.loadingviewfinal.RecyclerViewFinal;
import event.NetErrorEvent;
import event.RankingEvent;
import ui.activity.profit.biz.RankingBiz;
import ui.activity.profit.component.DaggerRankingComponent;
import ui.activity.profit.contract.RankingContract;
import ui.activity.profit.module.RankingModule;
import ui.activity.profit.presenter.RankingPresenter;
import ui.adapter.RankingAdapter;
import ui.model.RankingBean;
import util.StringUtils;
import util.SysUtils;

/**
 * Created by chenjian on 18/2/21.
 * <p>
 * 1
 */

public class Ranking2Fra extends BaseFra implements RankingContract.View {


    @Inject
    RankingPresenter presenter;
    @Inject
    RankingBiz biz;


    @BindView(R.id.pull_refresh_rv)
    RecyclerViewFinal rv;
    @BindView(R.id.pull_refresh_ptr)
    PtrClassicFrameLayout ptr;

    RankingAdapter adp;
    String strData="";


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.pull_refresh_layout, container, false);
        ButterKnife.bind(this, view);
        DaggerRankingComponent.builder().rankingModule(new RankingModule(this)).build().inject(this);
        presenter.setBiz(biz);
        strData=SysUtils.sf2.format(new Date(System.currentTimeMillis()));
        rv.setLayoutManager(new LinearLayoutManager(getActivity()));
        rv.setItemAnimator(new DefaultItemAnimator());
//        rv.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        ptr.setLastUpdateTimeRelateObject(this);
        ptr.disableWhenHorizontalMove(true);

        rv.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
                presenter.getInfo(false, 2, strData);
            }
        });

        ptr.setOnRefreshListener(new OnDefaultRefreshListener() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                presenter.getInfo(true,  2, strData);
            }
        });
        rv.setOnItemClickListener(new HeaderAndFooterRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView.ViewHolder holder, int position) {

            }
        });
        ptr.autoRefresh(true);
        return view;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        biz.onDestory();
        ButterKnife.bind(getActivity()).unbind();
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRankingEvent(RankingEvent event) {
        if (StringUtils.isEmpty(event.getStrData())) {
            return;
        }
        strData=event.getStrData();
        presenter.getInfo(true,  2, strData);
    }


    @Override
    public void upDateUI(List<RankingBean.ListBean> bean, int type) {
        if (adp == null) {
            adp = new RankingAdapter(getActivity(), bean, type);
            rv.setAdapter(adp);
        } else {
            adp.notifyDataSetChanged();
        }
    }

    @Override
    public void refreshComplete() {
        ptr.onRefreshComplete();
    }


    @Override
    public void loadMoreComplete() {
        rv.onLoadMoreComplete();
    }

    @Override
    public void hasLoadMore(boolean b) {
        rv.setHasLoadMore(b);
    }
}
